package com.csg.ses.service.ses.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csg.ses.common.utils.CommonUtil;
import com.csg.ses.common.utils.MybatisPlusUtil;
import com.csg.ses.common.utils.SnowFlakeUtil;
import com.csg.ses.dao.ses.SysRoleAccountsDao;
import com.csg.ses.entity.ses.SysRoleAccountsEntity;
import com.csg.ses.service.ses.SysRoleAccountsService;
import com.csg.ses.vo.common.Result;
import com.csg.ses.vo.common.ResultStatus;
import com.csg.ses.vo.list.RoleAccountsVo;
import com.csg.ses.vo.login.UserContext;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * 角色账户
 * @author wangjun
 * @date 2020/11/16
 */
@Service
@RequiredArgsConstructor
public class SysRoleAccountsServiceImpl extends ServiceImpl<SysRoleAccountsDao, SysRoleAccountsEntity> implements SysRoleAccountsService {
    private final SysRoleAccountsDao sysRoleAccountsDao;

    @Override
    public Result retrieve(SysRoleAccountsEntity account, Long current, Long size) {
        try {
            Page<SysRoleAccountsEntity> page = MybatisPlusUtil.initPage(current, size, true);
            QueryWrapper<SysRoleAccountsEntity> qw = new QueryWrapper();
            if (!CommonUtil.isEmpty(account.getRoleId())) {
                qw.eq("role_id", account.getRoleId());
            }
            qw.orderByAsc("role_id", "login_id");
            return new Result(sysRoleAccountsDao.retrieve(page, qw), ResultStatus.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultStatus.FAILED);
        }
    }

    @Override
    @Transactional
    public String del(RoleAccountsVo accountsVo) {
        try {
            List<SysRoleAccountsEntity> accountList = accountsVo.getRoleAccountsList();
            List<String> idList = new ArrayList<String>();
            for (SysRoleAccountsEntity entity : accountList) {
                QueryWrapper<SysRoleAccountsEntity> qw = new QueryWrapper();
                qw.eq("role_id", entity.getRoleId());
                qw.eq("login_id", entity.getLoginId());
                SysRoleAccountsEntity data = sysRoleAccountsDao.selectOne(qw);
                if (data != null) idList.add(data.getId());
            }
            if (idList != null && idList.size() > 0) {
                int num = sysRoleAccountsDao.deleteBatchIds(idList);
                return num + "条记录删除成功！";
            } else {
                return "未指定需要删除的记录！";
            }
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }

    @Override
    public Result getById(String id) {
        if (!CommonUtil.isEmpty(id)) {
            return new Result(sysRoleAccountsDao.selectById(id), ResultStatus.SUCCESS);
        } else {
            throw new RuntimeException("id不能为空！");
        }
    }

    @Override
    @Transactional
    public String save(RoleAccountsVo accountsVo) {
        try {
            if (accountsVo != null && accountsVo.getRoleAccountsList().size() > 0) {
                for (SysRoleAccountsEntity accounts : accountsVo.getRoleAccountsList()) {
                    accounts.setModifyTime(new Timestamp(System.currentTimeMillis()));
                    accounts.setModifiedBy(UserContext.getUserView().getLoginCode());
                    if (!CommonUtil.isEmpty(accounts.getId())) {    //更新
                        sysRoleAccountsDao.updateById(accounts);
                    } else {    //新增
                        if (sysRoleAccountsDao.existsCount(accounts.getRoleId(), accounts.getLoginId()) == 0) {
                            accounts.setId(SnowFlakeUtil.nextStringId());
                            accounts.setCreateTime(new Timestamp(System.currentTimeMillis()));
                            accounts.setCreatedBy(UserContext.getUserView().getLoginCode());
                            sysRoleAccountsDao.insert(accounts);
                        }
                    }
                }
                return "保存成功！";
            } else {
                return "未传入需要保存的记录！";
            }
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }
}